package com.sumainfor.pc.cfo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.pc.cfo.dao.BalanceMapper;
import com.sumainfor.pc.cfo.feign.BaseGmioFeign;
import com.sumainfor.pc.cfo.service.HouseRentService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Author Eddy·Chen
 * @Email 835033913@qq.com
 * @Create 2019-04-18 10:29
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Log4j2
public class HouseRentServiceImpl implements HouseRentService {

    @Autowired
    private BaseGmioFeign gmioFeign;


    @Autowired
    private BalanceMapper balanceMapper;

    /**
     * 查询唐巢收入信息 房租 公共事业 维修 夜间开门 退租 其他 主页数据
     *
     * @param page
     * @param params
     * @return
     */
    @Override
    public JsonResult selectOrderInTableList(Page page, Map<String, Object> params) {
        params.put("size", page.getSize());
        params.put("current", page.getCurrent());
        return gmioFeign.selectOrderInTableList(params);
    }

    /**
     * 查询唐巢支出账单
     * 账单类型  1.业主   2.供应商  3.退租账单   4.公共事业费
     *
     * @param page
     * @param params
     * @return
     */
    @Override
    public JsonResult selectOrderOutTableList(Page page, Map<String, Object> params) {
        params.put("size", page.getSize());
        params.put("current", page.getCurrent());
        JsonResult<JSONObject> result = gmioFeign.selectOrderOutTableList(params);
        if (result.isSuccess()) {
            if (MapUtils.getInteger(params, "BiOutType").equals(2)) {
                List<Map> list = result.getResult().getJSONArray("records").toJavaList(Map.class);
                for (Map<String, Object> map : list) {
                    String supId = MapUtils.getString(map, "SupId");
                    //查询前期余额
                    map.put("LastMoney", 0);
                    if (StringUtils.isNotBlank(supId)) {
                        Map<String, Object> balanceOne = balanceMapper.getBalanceOne(supId);
                        if (MapUtils.isNotEmpty(balanceOne)) {
                            map.put("LastMoney", balanceOne.get("PaymentDays"));
                        }
                    }
                }
            }
        }
        return result;
    }



    /**
     * 唐巢支付 设置支付成功接口
     *
     * @param biOutId
     * @return
     */
    @Override
    public JsonResult setPayDone(String biOutId) {
        return gmioFeign.setPayDone(biOutId);
    }

}
